/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WekaModels;

import grex.Data.ArffTableModel;
import grex.ErrorManager;
import grex.Prediction;
import grex.PredictionContainer;
import java.util.ArrayList;
import java.util.StringTokenizer;
import weka.classifiers.functions.LinearRegression;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.classifiers.rules.ZeroR;
import weka.core.Instance;
import weka.core.Instances;

/**
 *
 * @author RIK
 */
public class GrexWekaEnsemble extends WekaPredictiveModel{
    private ArrayList<WekaPredictiveModel> members = new ArrayList<WekaPredictiveModel>();
    public GrexWekaEnsemble(ArffTableModel data){
        super(data,null); 

    }

    public double getNrOfNodes() {
        return 1;
    }
    
    public void addMember(WekaPredictiveModel member){
        members.add(member);
    }

    public String getName() {
        return "Ensemble";
    }
    
   public void execute(PredictionContainer pc) {
        for (Prediction p : pc.values()) {

            try {
                Instance instance = wekaArffTableModel.getInstance(p.getInstance(),wekaTrain);//wekaTrain is just used to set the Dataset in the instance
                double prediction=0;
                for(WekaPredictiveModel member : members){
                    prediction += Math.max(member.model.classifyInstance(instance)/members.size(),0);
                    p.setProbs(member.model.distributionForInstance(instance));
                }
                
                p.setPrediction(prediction);
             
            } catch (Exception ex) {
                ErrorManager.getInstance().reportError(ex);
            }

        }
    }
    
    public void train() {
        try {
            for(WekaPredictiveModel member:members){
                member.model.buildClassifier(wekaTrain);
            }
            execute(pcTrain);
        } catch (Exception ex) {
            ErrorManager.getInstance().reportError(ex);
        }
    }    
    
    protected int calcNumberOfNodes(String graph) {
       return members.size();
    }
    
}
